package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.RatePlanModeDealAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description : SIM卡激活承诺期(天)200004032、到期续约模式200004026属性联动处理类
 * @Author : wei.yanhong
 * @Date : 2020/11/22 17:29
 */
@Service
@AttrHandler(RatePlanModeDealAttrHandler.class)
public class RatePlanModeDealLinkAttrHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    IOperateSceneInstService operateSceneInstService;
    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

        // 如果是到期续约模式200004026 需要联动设置指定续约计划 必填，显示
        if ("200004026".equals(attr.getFieldName())) {
            KiteAttr attr1 = inst.getAttr("200004027");
            if (Objects.nonNull(attr1) && "100006987".equals(attr.getValue())) {
                attr1.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_M);
                attr1.setIsNull(KeyConsts.IFTRUE_F);
                attr1.setIsEdit(KeyConsts.IFTRUE_T);
                if (inst instanceof KiteProdInst) {
                    deal(attr1, inst, data);
                }

            }
            else {
                attr1.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_E);
                attr1.setIsNull(KeyConsts.IFTRUE_T);
                attr1.setValue("");
                attr1.setValueDesc("");
            }
        }
        operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
    }

    /**
     * @param attr 指定续约计划属性
     * @param inst
     * @param data
     */
    private void deal(KiteAttr attr, KiteInst inst, AttrData data) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        // 处理指定续约计划下拉值，从当前实例下获取已经创建好的资费计划
        List<KiteProdInstFunc> kiteProdInstFuncs = new ArrayList<>();

        List<KiteProdInst> prodInsts = querySceneInstService.listProdInsts(data.getSceneInstId());
        if (KiteListUtils.isEmpty(prodInsts)){
            return;
        }
        for (KiteProdInst prodInst : prodInsts) {
            if (ActionType.A != prodInst.getActionType()) {
                continue;
            }
            List<KiteProdInstFunc> funcs = querySceneInstService
                .listProdInstFuncs(data.getCustId(), prodInst.getProdInstId());
            if (KiteListUtils.isNotEmpty(funcs)) {
                kiteProdInstFuncs.addAll(funcs);
            }
        }

        if (!KiteListUtils.isEmpty(kiteProdInstFuncs)) {

            kiteProdInstFuncs = kiteProdInstFuncs.stream().filter(t -> !ActionType.D.equals(t.getActionType()))
                .collect(Collectors.toList());
            List<KiteAttrValue> attrValues = new ArrayList<>();

            kiteProdInstFuncs.forEach(funcProdInst -> {
                // 200003698 资费计划名称
                KiteAttr nameAttr = funcProdInst.getAttr("200004129");
                if (Objects.nonNull(nameAttr)) {
                    KiteAttrValue attrValue = new KiteAttrValue();
                    attrValue.setAttrId(attr.getAttrId());
                    attrValue.setAttrNbr(attr.getAttrNbr());
                    attrValue.setAttrValue(nameAttr.getValue());
                    attrValue.setAttrValueName(nameAttr.getValue());
                    attrValues.add(attrValue);
                }
            });
            if (KiteListUtils.isNotEmpty(attrValues)) {
                //默认第一个
                attr.setValue(attrValues.get(0).getAttrValue());
                attr.setValueDesc(attrValues.get(0).getAttrValue());
                attr.setAttrValues(attrValues);
                operateInstAttrService.setAttrValue(inst, attr.getFieldName(), attrValues.get(0).getAttrValue(), data.getSceneInstId(), null);
            }
            operateSceneInstService.setAttr(data.getSceneInstId(), inst.getInstType(), inst.getInstId(), attr);
        }
    }
}
